package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.DaRenAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.DaRenBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.DialogFilerInter;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.FilterDialog;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NearByActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    @BindView(R.id.title_right_one_btn)
    ImageView titleRightOneBtn;
    @BindView(R.id.title_right_two_btn)
    ImageView titleRightTwoBtn;

    private DaRenAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private int page = 1;
    private String minages="",maxages="",longi="",lat="",distances="",labeids="",sexs="",search="", filterTimes="";
    private String labelclassid="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_by2);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        longi = (String) SPUtils.get(this, Constant.LOCATION_LON, "116.395645");
        lat = (String) SPUtils.get(this, Constant.LOCATION_LAT, "39.929985");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleRightOneBtn.setVisibility(View.VISIBLE);
        titleRightTwoBtn.setVisibility(View.VISIBLE);

        titleRightOneBtn.setImageResource(R.mipmap.icon_shaixuan);
        titleRightTwoBtn.setImageResource(R.mipmap.icon_sousuo);
        tvMiddleTitle.setText("附近");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new DaRenAdapter(this);
        mAdapter.setDataList(new ArrayList<DaRenBean.BodyBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.size_1)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.bg)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getDatas();
            }
        });
        recyclerView.forceToRefresh();
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i = new Intent(NearByActivity.this, AttentionUserClassifyActivity.class);
                i.putExtra("listId", mAdapter.getDataList().get(position).getListid() + "");
                startActivity(i);
            }
        });
    }

    private void refresh() {
        page = 1;
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();//必须调用此方法
        getDatas();
    }

    private void getDatas() {
        OkgoUtil.post(Http.CLASS_WORK_LSIT, NearByActivity.this)
                .params("pageno", page)
                .params("long", longi)
                .params("lat", lat)
                .params("minage", minages)
                .params("maxage", maxages)
                .params("distance", distances)
                .params("labelclassid", labelclassid)
                .params("labelid", labeids)
                .params("sex", sexs)
                .params("search", search)
                .params("time", filterTimes)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DaRenBean parentBean = DealResult.getInstance().dealDatas(NearByActivity.this, response, new TypeToken<DaRenBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            List<DaRenBean.BodyBean> beans = parentBean.getBody();
                            if (beans.isEmpty()) {
                                return;
                            }
                            mAdapter.addAll(beans);
                            recyclerView.refreshComplete(mAdapter.getItemCount());
                        } else {
                            if (code == Constant.EMPTY) {
                                if (page == 1) {
                                    ToastUtils.toastForShort(NearByActivity.this, "数据为空");
                                } else {
                                    recyclerView.setNoMore(true);
                                }
                            } else {
                                ToastUtils.toastForShort(NearByActivity.this, parentBean.getResult());
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(NearByActivity.this, getString(R.string.bad_net));
                    }
                });
    }


    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title, R.id.title_right_one_btn,R.id.title_right_two_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
            case R.id.title_right_one_btn:
                FilterDialog dialog = new FilterDialog(this, new DialogFilerInter() {
                    @Override
                    public void callBack(String datas, String labeid, String sex, String minage, String maxage, String distance,String filterTime) {
                        labeids=labeid;
                        sexs=sex;
                        minages=minage;
                        maxages=maxage;
                        distances=distance;
                        search="";
                        labelclassid=datas;
                        filterTimes=filterTime;
                        page=1;
                        mAdapter.clear();
                        lRecyclerViewAdapter.notifyDataSetChanged();
                        getDatas();
                    }
                });
                dialog.show();
                break;
            case R.id.title_right_two_btn:
                startActivity(new Intent(this, SearchActivity.class));
                break;
        }
    }
}
